<?php
	class ControllerCorePostcontent extends Controller
	{
		function index()
		{	
			$this->load->model("core/user");
			$this->load->model("core/media");
			$this->load->model("core/sitemap");
			$this->load->model("core/file");
			$this->load->helper('image');
			
			if (!$this->user->isLogged()) {
				$this->redirect($this->url->https('page/index'));
			}
			
			$this->getForm();
			
			$this->id='post';
			$this->template='core/post_form.tpl';	
			$this->render();
		}
		
		private function getForm()
		{
			$route = $this->getRoute();
			$sitemapid = $this->request->get['sitemapid'];
			$mediaid = $this->request->get['mediaid'];
			$siteid = $this->user->getSiteId();

			$this->load->language($route);
			
			$this->data = array_merge($this->data, $this->language->getData());
			
			$sitemap = $this->model_core_sitemap->getItem($sitemapid, $siteid);		
			
			//Save
			if (($this->request->post) && ($this->validate())) {
				$this->data['post'] = $this->request->post;
				$this->savepost();
			}
			else
			{
				if($route == "module/information")
				{
					$this->data['post'] = $this->model_core_media->getInformationMedia($sitemapid, "information");
					if($this->data['post']['title'] == '' && $route='module/information')
					{
						$this->data['post']['mediaparent'] = "module/information";
						$this->data['post']['title'] = $sitemap['sitemapname'];
					}
				}
				elseif($route == "module/contact")
				{
					$this->data['post'] = $this->model_core_media->getInformationMedia($sitemapid, "contact");
					if($this->data['post']['title'] == '' && $route='module/contact')
					{
						$this->data['post']['mediaparent'] = "module/contact";
						$this->data['post']['title'] = $sitemap['sitemapname'];
					}
					
					$this->data['post']['email1'] = $this->model_core_media->getInformation($this->data['post']['mediaid'], "email1");
					$this->data['post']['email2'] = $this->model_core_media->getInformation($this->data['post']['mediaid'], "email2");
					$this->data['post']['email3'] = $this->model_core_media->getInformation($this->data['post']['mediaid'], "email3");
				}
				else
				{
					$this->data['post'] = $this->model_core_media->getItem($mediaid);
				}
			}
			//Thiet ke form	
			$this->data['displaynews'] = "";
			//$this->data['heading_title'] = "Post News Page";
			$this->data['DIR_UPLOADPHOTO'] = HTTP_SERVER."index.php?route=common/uploadpreview";
			$this->data['DIR_UPLOADATTACHMENT'] = HTTP_SERVER."index.php?route=common/uploadattachment";
			$this->data['hasEmail'] = false;
			$this->data['hasTabMap'] = true;
			
			$this->data['DIR_CANCEL'] = HTTP_SERVER."index.php?route=".$route."&sitemapid=".$sitemapid;
			if($route == "module/information")
			{
				$this->data['displaynews'] = "display:none";
				$this->data['heading_title'] = $sitemap['sitemapname'];
				$this->data['hasTabMap'] = false;
				//$this->data['post']['title'] = $sitemap['sitemapname'];
				$this->data['DIR_CANCEL'] = HTTP_SERVER."index.php";
			}
			elseif($route == "module/contact")
			{
				$this->data['hasEmail'] = true;
				$this->data['hasTabMap'] = false;
				$this->data['displaynews'] = "display:none";
				$this->data['heading_title'] = "Contact Form";
				$this->data['DIR_CANCEL'] = HTTP_SERVER."index.php";
			}
			
		
			
			
			//Lay Breadcrumb
			$this->data['breadcrumb'] = $this->model_core_sitemap->getBreadcrumb($sitemapid, $siteid, -1);
			
			
			//get Form
			if($this->data['post']['imagepath'] != "")
			{
				$this->data['imagethumbnail'] = HelperImage::resizePNG($this->data['post']['imagepath'], 200, 200);
			}
			
			$this->data['mediaid'] = $this->data['post']['mediaid'];
			
			$this->data['title'] = $this->data['post']['title'];
			$this->data['summary'] = $this->data['post']['summary'];
			$this->data['description'] = $this->data['post']['description'];
			$this->data['author'] = $this->data['post']['author'];
			$this->data['source'] = $this->data['post']['source'];
			$this->data['refersitemap'] = $this->data['post']['refersitemap'];
			$this->data['imageid'] = $this->data['post']['imageid'];
			$this->data['imagepath'] = $this->data['post']['imagepath'];
			$this->data['fileid'] = $this->data['post']['fileid'];
			$this->data['filepath'] = $this->data['post']['filepath'];
			$this->data['groupkeys'] = $this->data['post']['groupkeys'];
			$this->data['viewcount'] = $this->data['post']['viewcount'];
			$this->data['position'] = $this->data['post']['position'];
			$this->data['status'] = $this->data['post']['status'];
			$this->data['statusdate'] = $this->data['post']['statusdate'];
			$this->data['statusby'] = $this->data['post']['statusby'];
			$this->data['updateddate'] = $this->data['post']['updateddate'];
			$listfile = $this->model_core_media->getInformation($this->data['mediaid'], "attachment");
			$listfileid=array();
			if($listfile)
				$listfileid=split(",",$listfile);
			$this->data['attachment']=array();
			foreach($listfileid as $key => $item)
			{
				$this->data['attachment'][$key] = $this->model_core_file->getFile($item);
				$this->data['attachment'][$key]['imagethumbnail'] = HelperImage::resizePNG($this->data['attachment'][$key]['filepath'], 50, 50);
				if(!$this->string->isImage($this->data['attachment'][$key]['extension']))
					$this->data['attachment'][$key]['imagethumbnail'] = DIR_IMAGE."icon/dinhkem.png";
			}
			$this->data['status'] = $this->data['post']['status'];
			if($this->data['status'] == "")
			{
				$this->data['status'] = "Active";
			}
			
			if($this->data['post']['refersitemap'] == "")
			{
				$this->data['post']['refersitemap'] .= "[".$sitemapid."]";
			}
			
			$this->data['listReferSiteMap'] = $this->getListSiteMapCheckBox($this->data['post']);
			
			if($route=="module/contact")
			{
				$this->data['email1'] = $this->data['post']['email1'];
				$this->data['email2'] = $this->data['post']['email2'];
				$this->data['email3'] = $this->data['post']['email3'];
			}
		}
		
		private function validate()
		{
			return true;
		}
		
		private function savepost()
		{
			$route = $this->getRoute();
			$sitemapid = $this->request->get['sitemapid'];
			$mediaid = $this->request->get['mediaid'];
			$siteid = $this->user->getSiteId();
				
			$sitemapid = $this->request->get['sitemapid'];
			
			
			$data['mediaid'] = $this->data['post']['mediaid'];
			
			$data['mediatype'] = "content";
			if($route == "module/product")
				$data['mediatype'] = "product";
			if($route == "module/information")
				$data['mediatype'] = "information";
			if($route == "module/contact")
				$data['mediatype'] = "contact";
			$data['userid'] = $this->user->getId();
			
			$data['title'] = $this->data['post']['title'];
			$data['summary'] = $this->data['post']['summary'];
			$data['description'] = $this->data['post']['description'];
			$data['author'] = $this->data['post']['author'];
			$data['source'] = $this->data['post']['source'];
			
			$data['status'] = $this->data['post']['status'];	
			$data['imageid'] = $this->data['post']['imageid'];
			$data['imagepath'] = $this->data['post']['imagepath'];
			
			$data['refersitemap'] = $this->data['post']['refersitemap'];
			//$data['refersitemap'] = $this->model_core_media->getReferSitemapString($sitemapid,$data['refersitemap']);
			
			$list = $this->model_core_sitemap->getListByModule("module/news",$this->user->getSiteId());
			foreach($list as $item)
			{
				$data['refersitemap'] =  $this->model_core_media->getReferSitemapString($item['sitemapid'], $data['refersitemap'], "delete");
			}
			
			$listrefersiteid = "";
			if($this->request->post['listrefersitemap'])
			{
				foreach ($this->request->post['listrefersitemap'] as $refersiteid) {
					$data['refersitemap'] .= "[".$refersiteid."]";
				}
			}
			$data['refersitemap'] = $this->model_core_media->getReferSitemapString($sitemapid, $data['refersitemap']);
			
			if($data['mediaid'] == "")
			{
				$data['mediaid'] = $this->model_core_media->insert($data);
			}
			else
			{
				if($this->model_core_media->update($data) == false)
				{
					exit("There are some problems, please contact administrator!");
				}
			}
			
			$listAttachment=$this->data['post']['attimageid'];
			$this->model_core_media->saveAttachment($data['mediaid'],$listAttachment);
			$listdelfile=$this->data['post']['delfile'];
			if(count($listdelfile))
				foreach($listdelfile as $item)
					$this->model_core_file->deleteFile($item);
			$this->model_core_media->clearTempFile();
			if($route=="module/contact")
			{
				$this->model_core_media->saveInformation($data['mediaid'], "email1", $this->data['post']['email1']);
				$this->model_core_media->saveInformation($data['mediaid'], "email2", $this->data['post']['email2']);
				$this->model_core_media->saveInformation($data['mediaid'], "email3", $this->data['post']['email3']);
			}
			
			//if($route == "module/news")
			//{
			$this->redirect("index.php?route=".$route."&sitemapid=".$sitemapid);
			//}
			//else
			//{
				//$this->redirect("index.php");
			//}
			
		}
		
		private function getListSiteMapCheckBox($media)
		{
			$strReferSitemap = $media['refersitemap'];
			$route = $this->getRoute();
			$list = $this->model_core_sitemap->getListByModule($route,$this->user->getSiteId());
			$html = "";
			foreach($list as $item)
			{
				$html .= '<tr>';
				$html .= '<td>';
				
				$sitemapid = "[".$item['sitemapid']."]";
				
				$pos = strrpos($strReferSitemap, $sitemapid);
				if ($pos === false) {
					$checked = "";
				}else{
					$checked = "checked=checked";
				}
				$html .= '<label><input name="listrefersitemap['.$item['sitemapid'].']" type="checkbox" value="'.$item['sitemapid'].'" '.$checked.'/> '.$item['sitemapname'].'</label>';
				$path = $this->model_core_sitemap->getBreadcrumb($item['sitemapid'], $this->user->getSiteId());
				$path = strip_tags($path);
				$html .= '</td><td>'.$path.'</td> </tr>';
			}
			
			return $html;
		}
		
	}
?>